#include "emotionmanager.h"
#include "abstractemotion.h"
#include "comicanimationemotionmanager.h"
#include "smileyanimationemotionmanager.h"
#include "trollanimationemotionmanager.h"

#include <QDebug>


namespace Emotion {

Manager *Manager::m_instance = NULL;

Manager::Manager(QObject *parent) :
    QObject(parent)
{
    init();
}


Manager::~Manager()
{
    m_emotionManagerList.clear();
    m_instance = NULL;
}


Manager *Manager::instance(QObject *parent)
{
    if (!m_instance)
        m_instance = new Manager(parent);
    return m_instance;
}


void Manager::init()
{
    registerEmotionManager(SmileyAnimationEmotionManager::instance());
    registerEmotionManager(TrollAnimationEmotionManager::instance());
    registerEmotionManager(ComicAnimationEmotionManager::instance());
}


void Manager::registerEmotionManager(AbstractEmotionManager *manager)
{
    if (manager == NULL)
        return;

    m_emotionManagerList.append(manager);
}


void Manager::unregisterEmotionManager(AbstractEmotionManager *manager)
{
    if (manager == NULL)
        return;

    m_emotionManagerList.removeAll(manager);
}


AbstractEmotionManager *Manager::at(int index) const
{
    if ((index < 0) || (index >= count()))
        return NULL;

    return m_emotionManagerList.at(index);
}


QString Manager::getCodeByName(const QString &name) const
{
    QString code = QString();
    for (int index = 0; index < count(); index++) {
        code = this->at(index)->getCodeByName(name);
        if (!code.isEmpty() && !code.isNull()) {
            qDebug() << code;
            return code;
        }
    }

    return code;
}


int Manager::count() const
{
    return m_emotionManagerList.count();
}


int Manager::size() const
{
    return m_emotionManagerList.size();
}


} // namespace Emotion
